{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import featuretools as ft\n",
    "from woodwork.logical_types import Categorical"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's load the data again\n",
    "\n",
    "df = pd.read_csv(\"retail.csv\", parse_dates=[\"invoice_date\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create and entity set\n",
    "\n",
    "es = ft.EntitySet(id=\"data\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Sole\\Documents\\Repositories\\envs\\fsml\\lib\\site-packages\\woodwork\\type_sys\\utils.py:40: UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format.\n",
      "  pd.to_datetime(\n",
      "C:\\Users\\Sole\\Documents\\Repositories\\envs\\fsml\\lib\\site-packages\\woodwork\\type_sys\\utils.py:40: UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format.\n",
      "  pd.to_datetime(\n",
      "C:\\Users\\Sole\\Documents\\Repositories\\envs\\fsml\\lib\\site-packages\\woodwork\\type_sys\\utils.py:40: UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format.\n",
      "  pd.to_datetime(\n",
      "C:\\Users\\Sole\\Documents\\Repositories\\envs\\fsml\\lib\\site-packages\\woodwork\\type_sys\\utils.py:40: UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format.\n",
      "  pd.to_datetime(\n",
      "C:\\Users\\Sole\\Documents\\Repositories\\envs\\fsml\\lib\\site-packages\\woodwork\\type_sys\\utils.py:40: UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format.\n",
      "  pd.to_datetime(\n",
      "C:\\Users\\Sole\\Documents\\Repositories\\envs\\fsml\\lib\\site-packages\\woodwork\\type_sys\\utils.py:40: UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format.\n",
      "  pd.to_datetime(\n"
     ]
    }
   ],
   "source": [
    "# Add the data to the entity\n",
    "\n",
    "es = es.add_dataframe(\n",
    "    dataframe=df,              # the dataframe with the data\n",
    "    dataframe_name=\"data\",     # unique name to associate with this dataframe\n",
    "    index=\"rows\",              # column name to index the items\n",
    "    make_index=True,           # if true, create a new column with unique values\n",
    "    time_index=\"invoice_date\", # column containing time data\n",
    "    logical_types={\n",
    "        \"customer_id\": Categorical, # the id is numerical, but should be handled as categorical\n",
    "    },\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Entityset: data\n",
       "  DataFrames:\n",
       "    data [Rows: 741301, Columns: 8]\n",
       "    invoices [Rows: 40505, Columns: 3]\n",
       "  Relationships:\n",
       "    data.invoice -> invoices.invoice"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a new dataframe with invoices\n",
    "# indicating its relationship to the main data\n",
    "\n",
    "es.normalize_dataframe(\n",
    "    base_dataframe_name=\"data\",     # Datarame name from which to split.\n",
    "    new_dataframe_name=\"invoices\",  # Name of the new dataframe.\n",
    "    index=\"invoice\",                # relationship will be created across this column.\n",
    "    copy_columns=[\"customer_id\"],   # columns to remove from base_dataframe and move to new dataframe.\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Sole\\Documents\\Repositories\\envs\\fsml\\lib\\site-packages\\featuretools\\primitives\\standard\\transform\\time_series\\numeric_lag.py:48: FutureWarning: NumericLag is deprecated and will be removed in a future version. Please use the 'Lag' primitive instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "([featuretools.primitives.standard.aggregation.n_most_common.NMostCommon,\n",
       "  featuretools.primitives.standard.aggregation.min_count.MinCount,\n",
       "  featuretools.primitives.standard.aggregation.max_consecutive_positives.MaxConsecutivePositives,\n",
       "  featuretools.primitives.standard.aggregation.mode.Mode,\n",
       "  featuretools.primitives.standard.aggregation.variance.Variance,\n",
       "  featuretools.primitives.standard.aggregation.n_unique_weeks.NUniqueWeeks,\n",
       "  featuretools.primitives.standard.aggregation.max_consecutive_zeros.MaxConsecutiveZeros,\n",
       "  featuretools.primitives.standard.aggregation.std.Std,\n",
       "  featuretools.primitives.standard.aggregation.count_less_than.CountLessThan,\n",
       "  featuretools.primitives.standard.aggregation.num_unique.NumUnique,\n",
       "  featuretools.primitives.standard.aggregation.count_above_mean.CountAboveMean,\n",
       "  featuretools.primitives.standard.aggregation.n_unique_days_of_calendar_year.NUniqueDaysOfCalendarYear,\n",
       "  featuretools.primitives.standard.aggregation.num_consecutive_greater_mean.NumConsecutiveGreaterMean,\n",
       "  featuretools.primitives.standard.aggregation.first_last_time_delta.FirstLastTimeDelta,\n",
       "  featuretools.primitives.standard.aggregation.num_peaks.NumPeaks,\n",
       "  featuretools.primitives.standard.aggregation.skew.Skew,\n",
       "  featuretools.primitives.standard.aggregation.n_most_common_frequency.NMostCommonFrequency,\n",
       "  featuretools.primitives.standard.aggregation.n_unique_days.NUniqueDays,\n",
       "  featuretools.primitives.standard.aggregation.count_inside_nth_std.CountInsideNthSTD,\n",
       "  featuretools.primitives.standard.aggregation.time_since_last.TimeSinceLast,\n",
       "  featuretools.primitives.standard.aggregation.count_outside_nth_std.CountOutsideNthSTD,\n",
       "  featuretools.primitives.standard.aggregation.n_unique_days_of_month.NUniqueDaysOfMonth,\n",
       "  featuretools.primitives.standard.aggregation.num_consecutive_less_mean.NumConsecutiveLessMean,\n",
       "  featuretools.primitives.standard.aggregation.median_count.MedianCount,\n",
       "  featuretools.primitives.standard.aggregation.avg_time_between.AvgTimeBetween,\n",
       "  featuretools.primitives.standard.aggregation.max_min_delta.MaxMinDelta,\n",
       "  featuretools.primitives.standard.aggregation.num_zero_crossings.NumZeroCrossings,\n",
       "  featuretools.primitives.standard.aggregation.is_unique.IsUnique,\n",
       "  featuretools.primitives.standard.aggregation.min_primitive.Min,\n",
       "  featuretools.primitives.standard.aggregation.sum_primitive.Sum,\n",
       "  featuretools.primitives.standard.aggregation.count_inside_range.CountInsideRange,\n",
       "  featuretools.primitives.standard.aggregation.kurtosis.Kurtosis,\n",
       "  featuretools.primitives.standard.aggregation.percent_unique.PercentUnique,\n",
       "  featuretools.primitives.standard.aggregation.count_outside_range.CountOutsideRange,\n",
       "  featuretools.primitives.standard.aggregation.time_since_last_min.TimeSinceLastMin,\n",
       "  featuretools.primitives.standard.aggregation.has_no_duplicates.HasNoDuplicates,\n",
       "  featuretools.primitives.standard.aggregation.count_below_mean.CountBelowMean,\n",
       "  featuretools.primitives.standard.aggregation.count_greater_than.CountGreaterThan,\n",
       "  featuretools.primitives.standard.aggregation.max_consecutive_negatives.MaxConsecutiveNegatives,\n",
       "  featuretools.primitives.standard.aggregation.average_count_per_unique.AverageCountPerUnique,\n",
       "  featuretools.primitives.standard.aggregation.median.Median,\n",
       "  featuretools.primitives.standard.aggregation.time_since_last_max.TimeSinceLastMax,\n",
       "  featuretools.primitives.standard.aggregation.n_unique_months.NUniqueMonths,\n",
       "  featuretools.primitives.standard.aggregation.is_monotonically_increasing.IsMonotonicallyIncreasing,\n",
       "  featuretools.primitives.standard.aggregation.time_since_first.TimeSinceFirst,\n",
       "  featuretools.primitives.standard.aggregation.mean.Mean,\n",
       "  featuretools.primitives.standard.aggregation.trend.Trend,\n",
       "  featuretools.primitives.standard.aggregation.is_monotonically_decreasing.IsMonotonicallyDecreasing,\n",
       "  featuretools.primitives.standard.aggregation.last.Last,\n",
       "  featuretools.primitives.standard.aggregation.max_count.MaxCount,\n",
       "  featuretools.primitives.standard.aggregation.count.Count,\n",
       "  featuretools.primitives.standard.aggregation.first.First,\n",
       "  featuretools.primitives.standard.aggregation.max_primitive.Max,\n",
       "  featuretools.primitives.standard.aggregation.entropy.Entropy],\n",
       " [featuretools.primitives.standard.transform.numeric.square_root.SquareRoot,\n",
       "  featuretools.primitives.standard.transform.datetime.is_first_week_of_month.IsFirstWeekOfMonth,\n",
       "  featuretools.primitives.standard.transform.binary.divide_by_feature.DivideByFeature,\n",
       "  featuretools.primitives.standard.transform.exponential.exponential_weighted_variance.ExponentialWeightedVariance,\n",
       "  featuretools.primitives.standard.transform.numeric.absolute.Absolute,\n",
       "  featuretools.primitives.standard.transform.binary.multiply_numeric_scalar.MultiplyNumericScalar,\n",
       "  featuretools.primitives.standard.transform.binary.equal.Equal,\n",
       "  featuretools.primitives.standard.transform.time_series.rolling_std.RollingSTD,\n",
       "  featuretools.primitives.standard.transform.binary.less_than_equal_to_scalar.LessThanEqualToScalar,\n",
       "  featuretools.primitives.standard.transform.exponential.exponential_weighted_average.ExponentialWeightedAverage,\n",
       "  featuretools.primitives.standard.transform.datetime.is_month_start.IsMonthStart,\n",
       "  featuretools.primitives.standard.transform.datetime.is_federal_holiday.IsFederalHoliday,\n",
       "  featuretools.primitives.standard.transform.numeric.negate.Negate,\n",
       "  featuretools.primitives.standard.transform.cumulative.cum_min.CumMin,\n",
       "  featuretools.primitives.standard.transform.binary.divide_numeric_scalar.DivideNumericScalar,\n",
       "  featuretools.primitives.standard.transform.savgol_filter.SavgolFilter,\n",
       "  featuretools.primitives.standard.transform.absolute_diff.AbsoluteDiff,\n",
       "  featuretools.primitives.standard.transform.numeric.natural_logarithm.NaturalLogarithm,\n",
       "  featuretools.primitives.standard.transform.datetime.day_of_year.DayOfYear,\n",
       "  featuretools.primitives.standard.transform.binary.not_equal_scalar.NotEqualScalar,\n",
       "  featuretools.primitives.standard.transform.time_series.expanding.expanding_count.ExpandingCount,\n",
       "  featuretools.primitives.standard.transform.binary.greater_than_equal_to_scalar.GreaterThanEqualToScalar,\n",
       "  featuretools.primitives.standard.transform.binary.not_equal.NotEqual,\n",
       "  featuretools.primitives.standard.transform.nth_week_of_month.NthWeekOfMonth,\n",
       "  featuretools.primitives.standard.transform.datetime.is_quarter_end.IsQuarterEnd,\n",
       "  featuretools.primitives.standard.transform.datetime.date_to_timezone.DateToTimeZone,\n",
       "  featuretools.primitives.standard.transform.binary.less_than_equal_to.LessThanEqualTo,\n",
       "  featuretools.primitives.standard.transform.time_series.numeric_lag.NumericLag,\n",
       "  featuretools.primitives.standard.transform.datetime.week.Week,\n",
       "  featuretools.primitives.standard.transform.datetime.weekday.Weekday,\n",
       "  featuretools.primitives.standard.transform.numeric.sine.Sine,\n",
       "  featuretools.primitives.standard.transform.datetime.is_lunch_time.IsLunchTime,\n",
       "  featuretools.primitives.standard.transform.binary.greater_than_scalar.GreaterThanScalar,\n",
       "  featuretools.primitives.standard.transform.binary.modulo_by_feature.ModuloByFeature,\n",
       "  featuretools.primitives.standard.transform.is_in.IsIn,\n",
       "  featuretools.primitives.standard.transform.datetime.is_working_hours.IsWorkingHours,\n",
       "  featuretools.primitives.standard.transform.datetime.hour.Hour,\n",
       "  featuretools.primitives.standard.transform.datetime.date_to_holiday.DateToHoliday,\n",
       "  featuretools.primitives.standard.transform.cumulative.cum_mean.CumMean,\n",
       "  featuretools.primitives.standard.transform.binary.less_than_scalar.LessThanScalar,\n",
       "  featuretools.primitives.standard.transform.time_series.rolling_outlier_count.RollingOutlierCount,\n",
       "  featuretools.primitives.standard.transform.binary.add_numeric.AddNumeric,\n",
       "  featuretools.primitives.standard.transform.datetime.distance_to_holiday.DistanceToHoliday,\n",
       "  featuretools.primitives.standard.transform.datetime.is_month_end.IsMonthEnd,\n",
       "  featuretools.primitives.standard.transform.time_series.expanding.expanding_trend.ExpandingTrend,\n",
       "  featuretools.primitives.standard.transform.binary.subtract_numeric_scalar.SubtractNumericScalar,\n",
       "  featuretools.primitives.standard.transform.binary.greater_than_equal_to.GreaterThanEqualTo,\n",
       "  featuretools.primitives.standard.transform.percent_change.PercentChange,\n",
       "  featuretools.primitives.standard.transform.binary.scalar_subtract_numeric_feature.ScalarSubtractNumericFeature,\n",
       "  featuretools.primitives.standard.transform.is_null.IsNull,\n",
       "  featuretools.primitives.standard.transform.datetime.month.Month,\n",
       "  featuretools.primitives.standard.transform.numeric.same_as_previous.SameAsPrevious,\n",
       "  featuretools.primitives.standard.transform.datetime.is_year_start.IsYearStart,\n",
       "  featuretools.primitives.standard.transform.time_series.rolling_trend.RollingTrend,\n",
       "  featuretools.primitives.standard.transform.binary.add_numeric_scalar.AddNumericScalar,\n",
       "  featuretools.primitives.standard.transform.time_series.lag.Lag,\n",
       "  featuretools.primitives.standard.transform.exponential.exponential_weighted_std.ExponentialWeightedSTD,\n",
       "  featuretools.primitives.standard.transform.numeric.tangent.Tangent,\n",
       "  featuretools.primitives.standard.transform.datetime.is_weekend.IsWeekend,\n",
       "  featuretools.primitives.standard.transform.cumulative.cum_sum.CumSum,\n",
       "  featuretools.primitives.standard.transform.time_series.expanding.expanding_max.ExpandingMax,\n",
       "  featuretools.primitives.standard.transform.datetime.part_of_day.PartOfDay,\n",
       "  featuretools.primitives.standard.transform.datetime.is_year_end.IsYearEnd,\n",
       "  featuretools.primitives.standard.transform.datetime.is_quarter_start.IsQuarterStart,\n",
       "  featuretools.primitives.standard.transform.time_series.expanding.expanding_min.ExpandingMin,\n",
       "  featuretools.primitives.standard.transform.binary.modulo_numeric.ModuloNumeric,\n",
       "  featuretools.primitives.standard.transform.time_series.rolling_count.RollingCount,\n",
       "  featuretools.primitives.standard.transform.binary.equal_scalar.EqualScalar,\n",
       "  featuretools.primitives.standard.transform.time_series.expanding.expanding_mean.ExpandingMean,\n",
       "  featuretools.primitives.standard.transform.datetime.day.Day,\n",
       "  featuretools.primitives.standard.transform.datetime.minute.Minute,\n",
       "  featuretools.primitives.standard.transform.time_series.rolling_max.RollingMax,\n",
       "  featuretools.primitives.standard.transform.datetime.season.Season,\n",
       "  featuretools.primitives.standard.transform.datetime.quarter.Quarter,\n",
       "  featuretools.primitives.standard.transform.time_series.rolling_mean.RollingMean,\n",
       "  featuretools.primitives.standard.transform.time_series.expanding.expanding_std.ExpandingSTD,\n",
       "  featuretools.primitives.standard.transform.binary.modulo_numeric_scalar.ModuloNumericScalar,\n",
       "  featuretools.primitives.standard.transform.numeric.cosine.Cosine,\n",
       "  featuretools.primitives.standard.transform.binary.subtract_numeric.SubtractNumeric,\n",
       "  featuretools.primitives.standard.transform.datetime.time_since.TimeSince,\n",
       "  featuretools.primitives.standard.transform.numeric.percentile.Percentile,\n",
       "  featuretools.primitives.standard.transform.datetime.second.Second,\n",
       "  featuretools.primitives.standard.transform.cumulative.cum_count.CumCount,\n",
       "  featuretools.primitives.standard.transform.binary.multiply_numeric.MultiplyNumeric,\n",
       "  featuretools.primitives.standard.transform.binary.divide_numeric.DivideNumeric,\n",
       "  featuretools.primitives.standard.transform.numeric.rate_of_change.RateOfChange,\n",
       "  featuretools.primitives.standard.transform.datetime.days_in_month.DaysInMonth,\n",
       "  featuretools.primitives.standard.transform.cumulative.cum_max.CumMax,\n",
       "  featuretools.primitives.standard.transform.datetime.year.Year,\n",
       "  featuretools.primitives.standard.transform.numeric.diff.Diff,\n",
       "  featuretools.primitives.standard.transform.binary.greater_than.GreaterThan,\n",
       "  featuretools.primitives.standard.transform.time_series.rolling_min.RollingMin,\n",
       "  featuretools.primitives.standard.transform.binary.less_than.LessThan,\n",
       "  featuretools.primitives.standard.transform.datetime.time_since_previous.TimeSincePrevious,\n",
       "  featuretools.primitives.standard.transform.datetime.is_leap_year.IsLeapYear])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# the operations between variables live in the transformation.binary module\n",
    "# the documentation is lagging at the time of writing, so we can\n",
    "# inspect the functions that are available like this:\n",
    "\n",
    "ft.get_valid_primitives(es, target_dataframe_name=\"data\", max_depth=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Feature: customer_id>,\n",
       " <Feature: stock_code>,\n",
       " <Feature: description>,\n",
       " <Feature: quantity>,\n",
       " <Feature: price>,\n",
       " <Feature: price * quantity>]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Obtain new variable \"amount\" by multiplying\n",
    "# price and quantity.\n",
    "\n",
    "feature_matrix, feature_defs = ft.dfs(\n",
    "    entityset=es,                          # the entity set\n",
    "    target_dataframe_name=\"data\",          # the dataframe for wich to create the feature\n",
    "    agg_primitives=[],                     # we need an empty list to avoid returning the defo parameters\n",
    "    trans_primitives=[\"multiply_numeric\"], # the operation to create the new features\n",
    "    primitive_options={                    # the features that we want to multiply\n",
    "        (\"multiply_numeric\"): {\n",
    "            'include_columns': {\n",
    "                'data': [\"quantity\", \"price\"]\n",
    "            }\n",
    "        }\n",
    "    },\n",
    "    ignore_dataframes=[\"invoices\"],\n",
    ")\n",
    "\n",
    "# display name of created features\n",
    "feature_defs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>stock_code</th>\n",
       "      <th>description</th>\n",
       "      <th>quantity</th>\n",
       "      <th>price</th>\n",
       "      <th>price * quantity</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rows</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>85048</td>\n",
       "      <td>15CM CHRISTMAS GLASS BALL 20 LIGHTS</td>\n",
       "      <td>12</td>\n",
       "      <td>6.95</td>\n",
       "      <td>83.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>79323P</td>\n",
       "      <td>PINK CHERRY LIGHTS</td>\n",
       "      <td>12</td>\n",
       "      <td>6.75</td>\n",
       "      <td>81.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>79323W</td>\n",
       "      <td>WHITE CHERRY LIGHTS</td>\n",
       "      <td>12</td>\n",
       "      <td>6.75</td>\n",
       "      <td>81.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>22041</td>\n",
       "      <td>RECORD FRAME 7\" SINGLE SIZE</td>\n",
       "      <td>48</td>\n",
       "      <td>2.10</td>\n",
       "      <td>100.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>21232</td>\n",
       "      <td>STRAWBERRY CERAMIC TRINKET BOX</td>\n",
       "      <td>24</td>\n",
       "      <td>1.25</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     customer_id stock_code                          description  quantity  \\\n",
       "rows                                                                         \n",
       "0        13085.0      85048  15CM CHRISTMAS GLASS BALL 20 LIGHTS        12   \n",
       "1        13085.0     79323P                   PINK CHERRY LIGHTS        12   \n",
       "2        13085.0     79323W                  WHITE CHERRY LIGHTS        12   \n",
       "3        13085.0      22041         RECORD FRAME 7\" SINGLE SIZE         48   \n",
       "4        13085.0      21232       STRAWBERRY CERAMIC TRINKET BOX        24   \n",
       "\n",
       "      price  price * quantity  \n",
       "rows                           \n",
       "0      6.95              83.4  \n",
       "1      6.75              81.0  \n",
       "2      6.75              81.0  \n",
       "3      2.10             100.8  \n",
       "4      1.25              30.0  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_matrix.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## In relation to pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>invoice</th>\n",
       "      <th>invoice_date</th>\n",
       "      <th>stock_code</th>\n",
       "      <th>description</th>\n",
       "      <th>quantity</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>85048</td>\n",
       "      <td>15CM CHRISTMAS GLASS BALL 20 LIGHTS</td>\n",
       "      <td>12</td>\n",
       "      <td>6.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>79323P</td>\n",
       "      <td>PINK CHERRY LIGHTS</td>\n",
       "      <td>12</td>\n",
       "      <td>6.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>79323W</td>\n",
       "      <td>WHITE CHERRY LIGHTS</td>\n",
       "      <td>12</td>\n",
       "      <td>6.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>22041</td>\n",
       "      <td>RECORD FRAME 7\" SINGLE SIZE</td>\n",
       "      <td>48</td>\n",
       "      <td>2.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>21232</td>\n",
       "      <td>STRAWBERRY CERAMIC TRINKET BOX</td>\n",
       "      <td>24</td>\n",
       "      <td>1.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   customer_id invoice        invoice_date stock_code  \\\n",
       "0      13085.0  489434 2009-12-01 07:45:00      85048   \n",
       "1      13085.0  489434 2009-12-01 07:45:00     79323P   \n",
       "2      13085.0  489434 2009-12-01 07:45:00     79323W   \n",
       "3      13085.0  489434 2009-12-01 07:45:00      22041   \n",
       "4      13085.0  489434 2009-12-01 07:45:00      21232   \n",
       "\n",
       "                           description  quantity  price  \n",
       "0  15CM CHRISTMAS GLASS BALL 20 LIGHTS        12   6.95  \n",
       "1                   PINK CHERRY LIGHTS        12   6.75  \n",
       "2                  WHITE CHERRY LIGHTS        12   6.75  \n",
       "3         RECORD FRAME 7\" SINGLE SIZE         48   2.10  \n",
       "4       STRAWBERRY CERAMIC TRINKET BOX        24   1.25  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# load data\n",
    "\n",
    "df = pd.read_csv(\"retail.csv\", parse_dates=[\"invoice_date\"])\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>invoice</th>\n",
       "      <th>invoice_date</th>\n",
       "      <th>stock_code</th>\n",
       "      <th>description</th>\n",
       "      <th>quantity</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>85048</td>\n",
       "      <td>15CM CHRISTMAS GLASS BALL 20 LIGHTS</td>\n",
       "      <td>12</td>\n",
       "      <td>6.95</td>\n",
       "      <td>83.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>79323P</td>\n",
       "      <td>PINK CHERRY LIGHTS</td>\n",
       "      <td>12</td>\n",
       "      <td>6.75</td>\n",
       "      <td>81.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>79323W</td>\n",
       "      <td>WHITE CHERRY LIGHTS</td>\n",
       "      <td>12</td>\n",
       "      <td>6.75</td>\n",
       "      <td>81.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>22041</td>\n",
       "      <td>RECORD FRAME 7\" SINGLE SIZE</td>\n",
       "      <td>48</td>\n",
       "      <td>2.10</td>\n",
       "      <td>100.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13085.0</td>\n",
       "      <td>489434</td>\n",
       "      <td>2009-12-01 07:45:00</td>\n",
       "      <td>21232</td>\n",
       "      <td>STRAWBERRY CERAMIC TRINKET BOX</td>\n",
       "      <td>24</td>\n",
       "      <td>1.25</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   customer_id invoice        invoice_date stock_code  \\\n",
       "0      13085.0  489434 2009-12-01 07:45:00      85048   \n",
       "1      13085.0  489434 2009-12-01 07:45:00     79323P   \n",
       "2      13085.0  489434 2009-12-01 07:45:00     79323W   \n",
       "3      13085.0  489434 2009-12-01 07:45:00      22041   \n",
       "4      13085.0  489434 2009-12-01 07:45:00      21232   \n",
       "\n",
       "                           description  quantity  price  amount  \n",
       "0  15CM CHRISTMAS GLASS BALL 20 LIGHTS        12   6.95    83.4  \n",
       "1                   PINK CHERRY LIGHTS        12   6.75    81.0  \n",
       "2                  WHITE CHERRY LIGHTS        12   6.75    81.0  \n",
       "3         RECORD FRAME 7\" SINGLE SIZE         48   2.10   100.8  \n",
       "4       STRAWBERRY CERAMIC TRINKET BOX        24   1.25    30.0  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Add total amount of transaction\n",
    "\n",
    "df[\"amount\"] = df[\"quantity\"].mul(df[\"price\"])\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fsml",
   "language": "python",
   "name": "fsml"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
